<template>
  <div>
      <el-upload
        class="upload-demo"
        ref="upload"
        :action="hrefUpload"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :on-success="successUpload"
        :file-list="fileList"
        :limit="limitUpload"
        :show-file-list="showUpload"
        :auto-upload="aotuUpload"
        :accept="acceptUpload">
        <el-button slot="trigger" size="small" class="pubBtnGree" type="primary">{{textUpload}}</el-button>
        <el-button style="margin-left: 10px;" v-if="!aotuUpload"  class="pubBtnGree" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
      </el-upload>
  </div>
</template>

<script>

export default {
  components: {
        
  },
  props:{
      acceptUpload:{  //上传文件格式,默认xlsx
         type:String,
         default:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
        //  application/vnd.android.package-archive   apk文件名形式
      },
      hrefUpload:{
          type:String,
          default:"https://jsonplaceholder.typicode.com/posts/"
      },
      limitUpload:{          //可上传个数
          type:Number,
          default:1,
      },
      aotuUpload:{       //是否自动上传服务器
          type:Boolean,
          default:true,
      },
      textUpload:{      //按钮文字
          type:String,
          default:"批量新增",
      },
      showUpload:{     //是否显示文字列表
          type:Boolean,
          default:false
      },
      fileListPro:{       //默认已有的图片数据
           type:Array,
           default:()=>[]
      },
      successFn:{         //上传成功后回调父组件的方法
          type:String,
          default:""
      }
  },
  data() {
      return {
        fileList: []
      };
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {


  },
  // 监控data中的数据变化
  watch: {
     fileListPro(newVal){
       this.fileList=newVal
     }

 },
  methods: {
  submitUpload() {
        this.$refs.upload.submit();
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      successUpload(res){
              this.fileList=[]
              this.$emit("successFn",res)
      }

  },
  created() {

  },
  mounted() {


  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='less' scoped>


</style>